package com.example.tsproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tsproject.entity.PageBean;
import com.example.tsproject.entity.Students;
import com.example.tsproject.entity.TeacherPosts;
import com.example.tsproject.service.StudentsService;
import com.example.tsproject.mapper.StudentsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @author DELL
* @description 针对表【students】的数据库操作Service实现
* @createDate 2024-09-24 21:13:24
*/
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students>
    implements StudentsService{

    @Autowired
    private StudentsMapper studentsMapper;

    @Override
    public Students findByEmail(String email) {
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email+"@qq.com");
        return this.getOne(queryWrapper);
    }

    @Override
    public Optional<Students> findStudentsJudge(String student_number, String password) {
        return studentsMapper.findStudentsJudge(student_number,password);
    }

    @Override
    public Students findStudents(String student_number) {
        return studentsMapper.findStudents(student_number);
    }

    @Override
    public List<Students> findStudentsByClass(String stuclass) {
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stuclass", stuclass);
        return this.list(queryWrapper);
    }

    @Override
    public PageBean<Students> findAllStudents(Integer pageNum, Integer pageSize, String name, String id, String stuClass) {
        // 创建 PageBean 对象
        PageBean<Students> pageBean = new PageBean<>();

        // 查询所有满足条件的学生数据
        List<Students> allStudents = studentsMapper.findAllStudents(name, id, stuClass);

        // 如果没有数据，直接返回空的 PageBean
        if (allStudents == null || allStudents.isEmpty()) {
            pageBean.setTotal(0L);
            pageBean.setItems(new ArrayList<>());
            return pageBean;
        }

        // 计算总页数
        int total = allStudents.size();
        int totalPages = (int) Math.ceil((double) total / pageSize);

        // 验证页码是否有效
        if (pageNum < 1 || pageNum > totalPages) {
            pageBean.setTotal((long) total);
            pageBean.setItems(new ArrayList<>());
            return pageBean;
        }

        // 计算当前页的起始索引和结束索引
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        // 截取当前页的数据
        List<Students> students = allStudents.subList(startIndex, endIndex);

        // 填充 PageBean 对象
        pageBean.setTotal((long) total);
        pageBean.setItems(students);

        return pageBean;
    }

    @Override
    public void delete(String id) {
        studentsMapper.delete(id);
    }

    @Override
    public void add(String studentNumber, String name, String password, String stuclass, String email) {
        studentsMapper.add(studentNumber, name, password, stuclass, email);
    }
}




